/*
${' '}* Copyright ${.now?string('yyyy')} The JA-SIG Collaborative. All rights reserved.
${' '}* distributed with this file and available online at
${' '}*/
package ${basePackage}.adapter.driving.persistence.${module}.repository;

import ${basePackage}.domain.${module}.${className};
import ${basePackage}.domain.${module}.repository.${className}Repository;
import ${basePackage}.adapter.driving.persistence.${module}.${className}Converter;
import ${basePackage}.adapter.driving.persistence.${module}.mapper.${className}Mapper;
import ${basePackage}.adapter.driving.persistence.${module}.po.${className}DO;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class ${className}RepositoryImpl implements ${className}Repository {

  private final ${className}Converter ${lowerName}Converter;
  private final ${className}Mapper ${lowerName}Mapper;

  @Override
  public Integer save(${className} ${lowerName}) {
    ${className}Po ${lowerName}Po = ${lowerName}Converter.toPo(${lowerName});
    if (${lowerName}.isNew()) {
      ${lowerName}Mapper.insert(${lowerName}Po);
      return ${lowerName}Po.getId();
    }
    return ${lowerName}Mapper.updateById(${lowerName}Po);
  }

  @Override
  public int deleteById(Integer id) {
    return ${lowerName}Mapper.deleteById(id);
  }

  @Override
  public Optional<${className}> findById(Integer id) {
    var ${lowerName}Po = ${lowerName}Mapper.selectById(id);
    return Optional.ofNullable(${lowerName}Converter.toDomain(${lowerName}Po));
  }
}
